import http from '@/common/http';
import { Dialog } from 'mand-mobile';
import Loading from '@/components/loading';
const defaultState = () => {
  return {
    WxWalltInfo: null,
    H5WxWalltInfo: null
  };
};
const state = defaultState();
const actions = {
  // 只用来通过接口或者异步处理
  WxWalltPayment(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('/recharge/rechargeWx', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setWalltPaymentInfo', res.data.data);
          resolve();
        } else if (res.data.code === '200008') {
          Dialog.alert({
            title: '错误',
            content: '请不要重复支付',
            onConfirm: () => {
              reject(new Error('200008'));
            }
          });
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  h5WxWalltPayment(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('/recharge/rechargeH5', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setH5WxWalltInfo', res.data.data);
          resolve();
        } else if (res.data.code === '200008') {
          Dialog.alert({
            title: '错误',
            content: '请不要重复支付',
            onConfirm: () => {
              reject(new Error('200008'));
            }
          });
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  }
};
const mutations = {
  // 修改state的数据。同步操作
  setWalltPaymentInfo(state, data) {
    state.WxWalltInfo = data;
  },
  setH5WxWalltInfo(state, data) {
    state.H5WxWalltInfo = data;
  }
};
const getters = {
  getWalltPaymentInfo(state) {
    return state.WxWalltInfo;
  },
  getH5WalltInfo(state) {
    return state.H5WxWalltInfo;
  }
};
export default {
  state,
  actions,
  mutations,
  getters
};
